package com.ljc.cjgl.service;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ljc.cjgl.entitiy.TbTest;
import com.ljc.cjgl.form.TestForm;
import com.ljc.cjgl.repository.TestRepository;

@Service
public class TestService {
	@Autowired
	private TestRepository testDAO;
	
	public List<TbTest> findAll(){
		return testDAO.findAll();
	}

	public TbTest findOne(Integer id) {
		Optional<TbTest> optional = testDAO.findById(id);
		if(optional.isPresent()) {
			return optional.get();
		}
		return null;
	}

	public Page<TbTest> findAll(Specification<TbTest> specification, Pageable pageRequest) {
		return testDAO.findAll(specification, pageRequest);
	}

	public synchronized void save(TestForm form) {
		TbTest model=new TbTest();
		if(form.getId()!=null) {
			model=testDAO.getOne(form.getId());
		}
		BeanUtils.copyProperties(form, model,"id");
		testDAO.save(model);
	}

	public List<TbTest> findByStatus(Integer i) {
		return testDAO.findByStatus(i);
	}

	public synchronized boolean delete(List<TbTest> tests) {
		try {
			for (TbTest test : tests) {
				testDAO.deleteById(test.getId());
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public List<TbTest> findByStatusLessThan(int i) {
		return testDAO.findByStatusLessThan(i);
	}
}
